using System.Collections.Generic;
using UnityEngine;

public class GlassFillingUpController : MonoBehaviour
{
	private List<Liquid> liquidsInGlass;

	private Liquid currentLiquid;

	private WaveGenerator waveGenerator;

	private Transform liquidLevelZero;

	private DrinkTargets drinkTargets;

	private bool wrongLiquidPanelShown;

	public void FillUpGlass(float value)
	{
		if (!CheckForRightLiquidType() && !wrongLiquidPanelShown)
		{
			Object.FindObjectOfType<GameController>().OnWrongLiquidPoured();
			wrongLiquidPanelShown = true;
		}
		if (currentLiquid != null)
		{
			currentLiquid.FillUpLiquid(value);
		}
	}

	public void FillUpGlassPerfectly(float targetHeight)
	{
		if (currentLiquid != null)
		{
			currentLiquid.FillUpLiquidPerfectlyToHeight(targetHeight);
		}
	}

	public void EndOfFillingUpGlass()
	{
		currentLiquid.ShineOnEndOfPouringLiquid();
		float num = 0f;
		float lastLiquidUpperPosition = GetLastLiquidUpperPosition();
		if (drinkTargets.GetCurrentTargetLine().IsLiquidLevelAboveScoredRange(lastLiquidUpperPosition))
		{
			num = 0f;
			Object.FindObjectOfType<GameController>().OnLoseLevel(1f, LoseReason.TOO_MUCH);
		}
		else if (drinkTargets.GetCurrentTargetLine().IsLiquidLevelBelowScoredRange(lastLiquidUpperPosition))
		{
			num = 0f;
			Object.FindObjectOfType<GameController>().OnLoseLevel(1f, LoseReason.TOO_LITTLE);
		}
		else
		{
			num = drinkTargets.GetCurrentTargetLine().GetScoreOfLiquidLevel(lastLiquidUpperPosition);
		}
		Object.FindObjectOfType<PouringPrecisionController>().SpawnBanner(num);
		if (CheckForRightLiquidType())
		{
			drinkTargets.SetNextCurrentTargetLine(lastLiquidUpperPosition);
		}
	}

	private bool CheckForRightLiquidType()
	{
		if (currentLiquid.type == drinkTargets.GetCurrentTargetLine().liquidType)
		{
			return true;
		}
		return false;
	}

	public bool IsGlassEmpty()
	{
		return currentLiquid == null || (currentLiquid != null && !currentLiquid.IsLiquidPoured());
	}

	public void ClearGlassFromLiquids()
	{
		foreach (Liquid item in liquidsInGlass)
		{
			UnityEngine.Object.Destroy(item.gameObject);
		}
		liquidsInGlass.Clear();
		currentLiquid = null;
		wrongLiquidPanelShown = false;
	}

	public Liquid GetLastLiquid()
	{
		if (liquidsInGlass.Count > 0)
		{
			return liquidsInGlass[liquidsInGlass.Count - 1];
		}
		return null;
	}

	public float GetLastLiquidUpperPosition()
	{
		Liquid lastLiquid = GetLastLiquid();
		Vector3 position = liquidLevelZero.position;
		float y = position.y;
		if (lastLiquid != null)
		{
			Vector3 liquidUpperPosition = lastLiquid.GetLiquidUpperPosition();
			y = liquidUpperPosition.y;
		}
		return y;
	}

	public float GetLiquidLevelZeroPosition()
	{
		Vector3 position = liquidLevelZero.position;
		return position.y;
	}

	public void AddLiquidToGlass(Liquid liquid)
	{
		liquidsInGlass.Add(liquid);
		currentLiquid = liquid;
		waveGenerator.CreateNewWaveBuffer(1);
		PassWaveBufferIndexesToLiquids();
		RecalculateWaveMultipliers();
	}

	public void RemoveEmptyLastLiquid()
	{
		UnityEngine.Object.Destroy(liquidsInGlass[liquidsInGlass.Count - 1].gameObject);
		liquidsInGlass.RemoveAt(liquidsInGlass.Count - 1);
		if (liquidsInGlass.Count > 0)
		{
			currentLiquid = liquidsInGlass[liquidsInGlass.Count - 1];
		}
		else
		{
			currentLiquid = null;
		}
		PassWaveBufferIndexesToLiquids();
		RecalculateWaveMultipliers();
	}

	private void RecalculateWaveMultipliers()
	{
		int count = liquidsInGlass.Count;
		float num = 0.2f / (float)count / (float)count / (float)count;
		for (int i = 0; i < count; i++)
		{
			float newLowerWaveMultiplier = num * (float)i * (float)i * (float)i;
			float newUpperWaveMultiplier = num * (float)(i + 1) * (float)(i + 1) * (float)(i + 1);
			liquidsInGlass[i].SetNewWaveMultipliers(newLowerWaveMultiplier, newUpperWaveMultiplier);
		}
	}

	private void PassWaveBufferIndexesToLiquids()
	{
		int count = liquidsInGlass.Count;
		for (int i = 0; i < count; i++)
		{
			liquidsInGlass[i].SetWaveBufferIndexes(i, i + 1);
		}
	}

	private void Awake()
	{
		DefineConnections();
	}

	private void Start()
	{
		liquidsInGlass = new List<Liquid>();
	}

	private void DefineConnections()
	{
		waveGenerator = GetComponent<WaveGenerator>();
		liquidLevelZero = base.transform.FindDeepChild("LiquidLevelZero");
		drinkTargets = GetComponent<DrinkTargets>();
	}
}
